$(function(){
  getHeight();
  //获取数据列表
  $.ajax({
    mrthod:'get',
    url: 'http://193.112.55.79:9090/api/getcouponproduct',
    data: {
      couponid:0
    },
    dataType:'json',
    success:function(res){
      if(res.result.length != 0){
        var imgs= [];
        for(var i=0;i<res.result.length;i++){
          var img = res.result[i].couponProductImg;
          if (img != "") {
            imgs.push(img);
          }
        }
        //渲染模板
        var html = template("app", res.result);
        $("#dataList").html(html);

        var banner_img = template("banner_img", imgs);
        $("#banner").html(banner_img);
      }
    }
  })

  //动态获取头部的高度
  window.onresize = function () {
    getHeight();
  }

  function getHeight() {
    setInterval(() => {
      var h = document.querySelector(".pageHeader").clientHeight-1;
      document.querySelector(".main").style.paddingTop = h + "px";
    }, 200);
  }

  //轮播图
   var swiper = new Swiper('.swiper-container', {
     effect: 'fade',
     grabCursor: true,
     pagination: {
      //  el: '.swiper-pagination',
     },
     navigation: {
       nextEl: '.swiper-button-next',
       prevEl: '.swiper-button-prev',
     },
    observer: true,
     //修改swiper自己或子元素时，自动初始化swiper    重要
    // observeParents: true, //修改swiper的父元素时，自动初始化swiper  重要

   });

   //动态给列表的a标签添加单击事件
   $("#dataList").on('click','a',function(){
     var index = $("#dataList a").index(this);
     swiper.activeIndex = index;
     swiper.update();
     var h = document.querySelector("html").scrollHeight;
     //将遮罩层和轮播图展示出来
     $("#priview").show();
      $("#model_big").show();
      document.querySelector("#model_big").style.height = h + "px";
     //取消页面滚动事件
   });
   //取消
    $("#model_big").click(function (event) {
       event.stopPropagation();
       $("#priview").hide();
       $("#model_big").hide();
    })
})